package com.szosen.measurement;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceView;

import com.szosen.jkzj.activity.BaseActivity;
import com.szosen.util.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.TreeMap;


public class ControlView {

	private SurfaceView sfv;
	private Canvas canvas;
	private Paint mPaint;
	public boolean drawFlag = true;
	private Context context;
	private DrawThread drawThread;
	public boolean isNeed = false;
	public boolean isStop = false; // 控制线程
	private ArrayList<Integer> usedlistV1;
	private ArrayList<Integer> usedlistV2;
	private ArrayList<Integer> usedlistV3;
	private ArrayList<Integer> usedlistV4;
	private ArrayList<Integer> usedlistV5;
	private ArrayList<Integer> usedlistV6;
	private ArrayList<Integer> usedlistI;
	private ArrayList<Integer> usedlistII;
	private ArrayList<Integer> usedlistIII;
	private ArrayList<Integer> usedlistaVR;
	private ArrayList<Integer> usedlistaVL;
	private ArrayList<Integer> usedlistaVF;
	private ArrayList<int[]> tmpValues;
	private ArrayList<Integer> usedlist;
	private static int SIZE = 10;
	private Handler updateUiHandler;
	public static final int PLUS_FLAG = 0x0000;
	public static final int DIALOG_FLAG = 0x0001;
	private Bitmap myScreenBitmap;
//	private Canvas myCanvas;
	public ArrayList<int[]> ecgData;

	/** 保存所有的ecgbytes的缓存队列集合 */
	public static MyEcgQueue<byte[]> ecgBytesQueue = new MyEcgQueue<byte[]>();
	public static MyEcgQueue<OneECG> oneECGQueue = new MyEcgQueue<OneECG>();

	public ControlView(Context context, SurfaceView sfv, Handler handler)
			throws IOException {
		this.sfv = sfv;
		this.context = context;
		// allEcgDatasShow=FileSaveUtil.getShowECGDatas(context);
		contentEcgShow = new ArrayList<>();
		myScreenBitmap = Bitmap
				.createBitmap(1280, 550, Bitmap.Config.ARGB_8888);
//		myCanvas = new Canvas(myScreenBitmap);
		mPaint = new Paint();
		mPaint.setColor(Color.rgb(0, 128, 64));
//		mPaint.setStrokeWidth(2);
		usedlistV1 = new ArrayList<Integer>();
		usedlistV2 = new ArrayList<Integer>();
		usedlistV3 = new ArrayList<Integer>();
		usedlistV4 = new ArrayList<Integer>();
		usedlistV5 = new ArrayList<Integer>();
		usedlistV6 = new ArrayList<Integer>();
		usedlistI = new ArrayList<Integer>();
		usedlistII = new ArrayList<Integer>();
		usedlistIII = new ArrayList<Integer>();
		usedlistaVR = new ArrayList<Integer>();
		usedlistaVL = new ArrayList<Integer>();
		usedlistaVF = new ArrayList<Integer>();
		tmpValues = new ArrayList<int[]>();
		usedlist = new ArrayList<Integer>();
		ecgBytesQueue = new MyEcgQueue<byte[]>();
		oneECGQueue = new MyEcgQueue<OneECG>();
		this.updateUiHandler = handler;
		try {
			drawThread = new DrawThread();
			drawThread.start();
			readQueueThread = new ReadQueueThread();
			readQueueThread.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
		drawFlag = true;
		System.out.println("执行到这个control了吗？");
	}

	/***
	 * 收集最后的数据包后放到显示的集合中。
	 *
	 * @param ecgData
	 */
	public void setEcgData(ArrayList<int[]> ecgData) {
		this.ecgData = ecgData;
		drawFlag = false;
		// ArrayList<int[]> temp = UploadECGBean.getLengthData(ecgData, 5120);
		// contentEcgShow = UploadECGBean.getSplitArray(ecgData,20);
		contentEcgShow = UploadECGBean.getMeiGeShuArray(ecgData, 8);
		gain = 0.7;
		listSize = 700 / 4 * 3;
		SIZE = ecgData.size();
		xWidth = 1.08;
	}

	/***
	 * 把所有属性配置都设置为结论的图形的结果。
	 */
	public void setResultConfig() {
		gain = 0.7 / 4;
		listSize = 700 / 4 * 3;
		xWidth = 1.08;
	}

	// public static ArrayList<OneECG> allOneECG = new ArrayList<OneECG>();

	public void addOneECG(OneECG one) {
		oneECGQueue.offer(one);
		// allOneECG.add(one);
	}

	/***
	 * 所有的用来显示假的心电数据的
	 */
	public static ArrayList<int[]> allEcgDatasShow;

	public void setEcgData2(ArrayList<int[]> ecgData) {
		this.ecgData = ecgData;
		// ArrayList<int[]> temp = UploadECGBean.getLengthData(ecgData, 5120);
		// contentEcgShow = UploadECGBean.getSplitArray(ecgData,20);
		contentEcgShow = ecgData;
		gain = 1;
		xWidth = 0.8;
		SIZE = ecgData.size();
		listSize = 500;
	}

	int[] tempInts = null;

	public void addResultECGData(final ArrayList<int[]> ecgData) {
		setResultConfig();
		cleanAllShowData();
		drawFlag = false;
		addECGDdataNow(ecgData);
		saveScreenshot();
	}

	/***
	 * 画一个图画。
	 */
	public void drawOneBackGround() {
		/*
		 * ArrayList<int[]> tempList = ecgData; if (contentEcgShow.size() == 12)
		 * { for (int i = 0; i < 12; i++) { // 先获取原来的数组的数据 tempInts =
		 * contentEcgShow.get(i); //每个导联的数据。 ArrayList<Integer> t =
		 * selectList(i); int[] now = ecgData.get(i); for(int
		 * j=0;i<now.length;j++){ t.add(now[j]); } // 合并出来的新的数据。 int[] newData =
		 * hebing(tempInts, now); // 然后复制加进来的ecgData的数据。
		 * System.out.println("控制的。"+Arrays.toString(newData));
		 * tempList.add(newData); } }
		 */
		// contentEcgShow.clear();
		// contentEcgShow.addAll(tempList);
		try {
			canvas = sfv.getHolder().lockCanvas();// 关键:获取画布
			if (canvas != null) {
				drawBackGround(canvas);
			}
		} catch (Exception e) {
		} finally {
			if (canvas != null) {
				sfv.getHolder().unlockCanvasAndPost(canvas);// 解锁画布，提交画好的图像
			}
		}
	}

	public void addECGDdataNow(final ArrayList<int[]> ecgData) {
		/*
		 * ArrayList<int[]> tempList = ecgData; if (contentEcgShow.size() == 12)
		 * { for (int i = 0; i < 12; i++) { // 先获取原来的数组的数据 tempInts =
		 * contentEcgShow.get(i); //每个导联的数据。 ArrayList<Integer> t =
		 * selectList(i); int[] now = ecgData.get(i); for(int
		 * j=0;i<now.length;j++){ t.add(now[j]); } // 合并出来的新的数据。 int[] newData =
		 * hebing(tempInts, now); // 然后复制加进来的ecgData的数据。
		 * System.out.println("控制的。"+Arrays.toString(newData));
		 * tempList.add(newData); } }
		 */
		// contentEcgShow.clear();
		// contentEcgShow.addAll(tempList);
		try {
			canvas = sfv.getHolder().lockCanvas();// 关键:获取画布
			if (canvas != null) {
				drawBackGround(canvas);
//				drawBackGround(myCanvas);
				drawValue2(ecgData, canvas);
			}
		} catch (Exception e) {
		} finally {
			if (canvas != null) {
				sfv.getHolder().unlockCanvasAndPost(canvas);// 解锁画布，提交画好的图像
			}
		}
	}

	/***
	 * 将两个int数组合并为一个数组。
	 *
	 * @param first
	 * @param second
	 * @return
	 */
	public static int[] hebing(int[] first, int[] second) {
		int[] result = Arrays.copyOf(first, first.length + second.length);
		System.arraycopy(second, 0, result, first.length, second.length);
		return result;
	}

	public ArrayList<Frame> getContent() {
		return null;
	}

	public void stop() {
		this.isStop = true;
		drawFlag = false;
		if (!drawThread.interrupted()) {
			drawThread.interrupt();
		}
		if (!readQueueThread.interrupted()) {
			readQueueThread.interrupt();
		}
	}

	public void start() {
		isStop = false;
		drawFlag = true;
	}

	/**
	 * 负责绘制data中的数据
	 *
	 * @author DC
	 *
	 */
	class DrawThread extends Thread {
		// private Canvas canvas;
		private long wait = 10;

		@Override
		public void run() {
			while (drawFlag) {
				// draw();
				// 有数据才画出来。
				// System.out.print("oneECGQueue是不是为空："+oneECGQueue.isEmpty());
				OneECG one = oneECGQueue.poll();
				if (one != null) {
					addECGDdataNow(one.getECG12());
				}
				try {
					Thread.sleep(wait);
				} catch (InterruptedException e) {
					e.printStackTrace();
					drawFlag = false;
					return;
				}
			}
		}
	}

	private TreeMap<String, Integer> report1;
	private UploadECGBean bean;

	class ReadQueueThread extends Thread {

		byte[] ecgBytes = new byte[373142 + 100];
		byte[] ecgBytesNow = new byte[2048 * 1000];
		int index = 0;
		boolean isGetData = false;
		int count = 0;
		boolean isGetDataNow = false;
		int indexNow = 0;
		int datalength = 0;

		@Override
		public void run() {
			while (!isStop) {
				if (!ecgBytesQueue.isEmpty()) {
					byte[] data = null;
					data = ecgBytesQueue.poll();
					if (data != null) {
						int length = data.length;
						count += length;
						// System.out.println("线程获取到数据："+StringUtils.ByteArrToHex(data));
						if (length >= 100) {
							if (data[0] == (byte) -69 && data[1] == (byte) -86) {
								// Toast.makeText(context, "开始收集数据。", 0).show();
								// Toast.makeText(context, "已经读取到了BBAA的数据。。。。",
								// 0).show();
								isGetDataNow = true;
								indexNow = 0;
								datalength = StringUtils.byte2intUnsigned(
										data[2], data[3]);
								// ecgBytesNow=new byte[2048*100];
								drawFlag = true;
							}
							if (isGetDataNow) {
								// 当小于长度就添加到一个字节包里面
								// 当是最后的标记时，就闯将对象
								for (int i = 0; i < length; i++) {
									if (i + 1 < length && data[i] == (byte) 49
											&& data[i + 1] == (byte) 96) {
										i++;
									} else {
										ecgBytesNow[indexNow] = data[i];
										indexNow++;
									}
								}
								// 这是获取到一个小包的数据末尾
								if (data[length - 1] == (byte) -35
										&& data[length - 2] == (byte) -52) {
									// System.out.println("CCCCCDDDDDD："+StringUtils.ByteArrToHex(data));
									// 已经获取到末尾的数据了。然后就生成一个代表一次的数据包的OneECG对象。放入到控制绘图的类中。
									byte[] bs = new byte[datalength];
									System.arraycopy(ecgBytesNow, 0, bs, 0,
											datalength);
									OneECG oneEcg = new OneECG(bs);
									// clv.setEcgData2(oneEcg.getECG12());
									// ControlView.flag = true;
									ControlView.this.addOneECG(oneEcg);
									// clv.addECGDdataNow(oneEcg.getECG12());
									updateUiHandler.obtainMessage(
											MeasureECGActivity.updateEcgXinLvNow,
											oneEcg.getXinlv() + "")
											.sendToTarget();
									// 已经不再获取数据了。
									isGetDataNow = false;
									// ecgBytesNow=new byte[2048*10];
								}
							}
							// System.out.println("数据count是：" + count +
							// ",index是："
							// + index);
						}
					}
				}
			}
		}
	}

	/***
	 * 返回截图的bitmap
	 *
	 * @return
	 */
	public Bitmap getScreenBitmap() {
		return myScreenBitmap;
	}

	/**
	 * 将surfaceView截图。先取得同大小的bitmap，然后
	 *
	 * @param saveName
	 */
	public void setSavePNGFileName(String pngFileName) {
		this.pngFileName = pngFileName;
	}

	private String pngFileName = "";

	public void saveScreenshot() {
		// doDraw(1, canvas);
		// if(TextUtils.isEmpty(saveName)){
		// saveName=""+System.currentTimeMillis();
		// }
		// //保存打印文件
		// String png = Environment.getExternalStorageDirectory() +
		// File.separator
		// + "新达启帆" + File.separator + "心电打印文档图片缓存" + File.separator
		// + BaseActivity.getUser().getName();
		//
		String png = Environment.getExternalStorageDirectory() + File.separator
				+ MeasureECGActivity.FileName + File.separator + "心电打印文档图片缓存"
				+ File.separator + BaseActivity.getHealthRecord().getPersonName();
		File path = new File(png);
		System.out.println("png文件名：" + pngFileName);
		if (!path.exists()) {
			path.mkdirs();
		}
		File file = new File(path.getAbsolutePath(), pngFileName.replace(":",
				"+") + ".png");
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file);
			myScreenBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
			// FileSaveUtil.savePDFFile("姓名 \n", myScreenBitmap,
			// pdfFileName);
			// FileSaveUtil.savePDFFile("姓名是\n", file.getAbsolutePath(),
			// pdfFileName);
			fos.close();
		} catch (FileNotFoundException e) {
			Log.e("Panel", "FileNotFoundException", e);
		} catch (IOException e) {
			Log.e("Panel", "IOEception", e);
		}
	}

	private boolean isScreenshoted = false;// 已经截图
	private int oneEcgIndex = 0;

	private void draw() {
		try {
			canvas = sfv.getHolder().lockCanvas();// 关键:获取画布
			if (canvas != null) {
				drawBackGround(canvas);
//				drawBackGround(myCanvas);
				// 收集到很多的点，每10个点打印一次就是说可能太多了。就不能打印出来。
				/*
				 * for (int i = 0; i < content.size(); i += 2.5) { //
				 * 收集到10个点打印一次； if (points < SIZE) { points++; } else {
				 * drawValue(tmpValues, canvas); // drawValue(tmpValues,
				 * myCanvas); points = 0; tmpValues.clear(); } }
				 */
				if (ecgData != null) {
					// ArrayList<int[] > list = new ArrayList<int[]>();
					// // SIZE = 300;
					// for(int i = 0;i<300;i++){
					// list.add(ecgData.get(i));
					// }
					// 获取前面的2000个数据。
					// ArrayList<int[]> split =
					// UploadECGBean.getLengthData(ecgData, 2000);
					// 然后取精度。每5个点娶一个元素。
					// ArrayList<int[]> temp =
					// UploadECGBean.getSplitArray(split, 5);
					// addECGDdataNow(allOneECG.get(oneEcgIndex).getECG12());
					oneEcgIndex++;
					// drawValue2(show, canvas);
				}
			}
		} catch (Exception e) {
		} finally {
			if (canvas != null) {
				sfv.getHolder().unlockCanvasAndPost(canvas);// 解锁画布，提交画好的图像
			}
		}
	}

	boolean hasDraw = false;

	private void drawValue2(ArrayList<int[]> values, Canvas canvas) {
		/********** 画心电图线 **********/
		/*
		 * int SIZE = 360; int index = 0; int[] tmpI = new int[SIZE]; int[]
		 * tmpII = new int[SIZE]; int[] tmpIII = new int[SIZE]; int[] tmpaVR =
		 * new int[SIZE]; int[] tmpaVL = new int[SIZE]; int[] tmpaVF = new
		 * int[SIZE]; int[] tmpV1 = new int[SIZE]; int[] tmpV2 = new int[SIZE];
		 * int[] tmpV3 = new int[SIZE]; int[] tmpV4 = new int[SIZE]; int[] tmpV5
		 * = new int[SIZE]; int[] tmpV6 = new int[SIZE];
		 * System.arraycopy(values.get(0), index, tmpI, 0, SIZE);
		 * System.arraycopy(values.get(1), index, tmpII, 0, SIZE);
		 * System.arraycopy(values.get(2), index, tmpIII, 0, SIZE);
		 * System.arraycopy(values.get(3), index, tmpaVR, 0, SIZE);
		 * System.arraycopy(values.get(4), index, tmpaVL, 0, SIZE);
		 * System.arraycopy(values.get(5), index, tmpaVF, 0, SIZE);
		 * System.arraycopy(values.get(6), index, tmpV1, 0, SIZE);
		 * System.arraycopy(values.get(7), index, tmpV2, 0, SIZE);
		 * System.arraycopy(values.get(8), index, tmpV3, 0, SIZE);
		 * System.arraycopy(values.get(9), index, tmpV4, 0, SIZE);
		 * System.arraycopy(values.get(10), index, tmpV5, 0, SIZE);
		 * System.arraycopy(values.get(11), index, tmpV6, 0, SIZE);
		 *
		 * drawECGGraph(75, tmpI, 1); // I drawECGGraph(155, tmpII, 2); // II
		 * drawECGGraph(235, tmpIII, 3); // III drawECGGraph(315, tmpaVR, 4); //
		 * aVR drawECGGraph(395, tmpaVL, 5); // aVL drawECGGraph(475, tmpaVF,
		 * 6); // aVF drawECGGraph(75, tmpV1, 7); // v1 drawECGGraph(155, tmpV2,
		 * 8); // v2 drawECGGraph(235, tmpV3, 9); // v3 drawECGGraph(315, tmpV4,
		 * 10); // v4 drawECGGraph(395, tmpV5, 11); // v5 drawECGGraph(475,
		 * tmpV6, 12); // v6
		 */
		drawECGGraph(75, values.get(0), 1); // I
		drawECGGraph(155, values.get(1), 2); // II
		drawECGGraph(235, values.get(2), 3); // III
		drawECGGraph(315, values.get(3), 4); // aVR
		drawECGGraph(395, values.get(4), 5); // aVL
		drawECGGraph(475, values.get(5), 6); // aVF
		drawECGGraph(75, values.get(6), 7); // v1
		drawECGGraph(155, values.get(7), 8); // v2
		drawECGGraph(235, values.get(8), 9); // v3
		drawECGGraph(315, values.get(9), 10); // v4
		drawECGGraph(395, values.get(10), 11); // v5
		drawECGGraph(475, values.get(11), 12); // v6
	}

	private void drawValue(ArrayList<int[]> values, Canvas canvas) {
		/********** 画心电图线 **********/
		// Log.i("int[] values", Arrays.toString(values));
		int[] tmpI = new int[SIZE];
		int[] tmpII = new int[SIZE];
		int[] tmpIII = new int[SIZE];
		int[] tmpaVR = new int[SIZE];
		int[] tmpaVL = new int[SIZE];
		int[] tmpaVF = new int[SIZE];
		int[] tmpV1 = new int[SIZE];
		int[] tmpV2 = new int[SIZE];
		int[] tmpV3 = new int[SIZE];
		int[] tmpV4 = new int[SIZE];
		int[] tmpV5 = new int[SIZE];
		int[] tmpV6 = new int[SIZE];

		for (int i = 0; i < SIZE; i++) {
			int[] tmp = values.get(i);
			tmpI[i] = tmp[0];
			tmpII[i] = tmp[1];
			tmpIII[i] = tmp[8];
			tmpaVR[i] = tmp[9];
			tmpaVL[i] = tmp[10];
			tmpaVF[i] = tmp[11];
			tmpV1[i] = tmp[2];
			tmpV2[i] = tmp[3];
			tmpV3[i] = tmp[4];
			tmpV4[i] = tmp[5];
			tmpV5[i] = tmp[6];
			tmpV6[i] = tmp[7];
		}
		drawECGGraph(75, tmpI, 1); // I
		drawECGGraph(155, tmpII, 2); // II
		drawECGGraph(235, tmpIII, 3); // III
		drawECGGraph(315, tmpaVR, 4); // aVR
		drawECGGraph(395, tmpaVL, 5); // aVL
		drawECGGraph(475, tmpaVF, 6); // aVF
		drawECGGraph(75, tmpV1, 7); // v1
		drawECGGraph(155, tmpV2, 8); // v2
		drawECGGraph(235, tmpV3, 9); // v3
		drawECGGraph(315, tmpV4, 10); // v4
		drawECGGraph(395, tmpV5, 11); // v5
		drawECGGraph(475, tmpV6, 12); // v6
	}

	public void cleanAllShowData() {
		usedlistI.clear();
		usedlistII.clear();
		usedlistIII.clear();
		usedlistaVR.clear();
		usedlistaVL.clear();
		usedlistaVF.clear();
		usedlistV1.clear();
		usedlistV2.clear();
		usedlistV3.clear();
		usedlistV4.clear();
		usedlistV5.clear();
		usedlistV6.clear();
	}

	private void drawBackGround(Canvas canvas) {
		/********** 画网格 **********/
		canvas.drawColor(Color.WHITE);
		mPaint.setColor(Color.GREEN);
		int height = canvas.getHeight();
		int width = canvas.getWidth();
		for (int u = 0; u < (width + 6); u += 6) {
			if (u % 30 == 0) {
				canvas.drawLine(u, 0, u, height, mPaint); // 纵轴方向 每隔30px即5mm
			} else {
				// 纵轴方向每隔6px打点即1mm
				for (int i = 0; i < (height + 6); i += 6)
					canvas.drawPoint(u, i, mPaint);
			}
		}
		for (int v = 0; v < (height + 30) / 30; v++) {
			canvas.drawLine(0, v * 30, width, v * 30, mPaint);
		}
		/********** 添加说明 **********/
		mPaint.setColor(Color.RED);
		int topMargin = 10, bottomMargin = 30, leftMargin = 20, rightMargin = 20;
		int stepWidth = (550 - topMargin - bottomMargin) / 6;
		int baseHeight = topMargin + stepWidth / 2;
		int baseWidth = leftMargin + (width - leftMargin - rightMargin) / 2;
		canvas.drawText("I", leftMargin, baseHeight, mPaint);
		canvas.drawText("II", leftMargin, baseHeight + stepWidth, mPaint);
		canvas.drawText("III", leftMargin, baseHeight + stepWidth * 2, mPaint);
		canvas.drawText("aVR", leftMargin, baseHeight + stepWidth * 3, mPaint);
		canvas.drawText("aVL", leftMargin, baseHeight + stepWidth * 4, mPaint);
		canvas.drawText("aVF", leftMargin, baseHeight + stepWidth * 5, mPaint);
		canvas.drawText("V1", baseWidth, baseHeight, mPaint);
		canvas.drawText("V2", baseWidth, baseHeight + stepWidth, mPaint);
		canvas.drawText("V3", baseWidth, baseHeight + stepWidth * 2, mPaint);
		canvas.drawText("V4", baseWidth, baseHeight + stepWidth * 3, mPaint);
		canvas.drawText("V5", baseWidth, baseHeight + stepWidth * 4, mPaint);
		canvas.drawText("V6", baseWidth, baseHeight + stepWidth * 5, mPaint);
	}

	public double xWidth = 1.4;
	public int listSize = 380;

	private void drawECGGraph(int base, int[] values, int tag) {

		/*
		 * for (int i = 0; i < values.length; i++) { values[i] = values[i] / 4;
		 * }
		 */
		mPaint.setColor(Color.BLACK);
		usedlist = selectList(tag);
		if (usedlist.size() > listSize - 2) {
			usedlist.clear();
		}
		if (tag < 7) {
			for (int i = 0; i < values.length && usedlist.size() < listSize; i++) {
				int y = convertY2Pix(base, values[i]);
				usedlist.add(y);
			}
			for (int j = 0; j < usedlist.size() - 1; j++) {
				canvas.drawLine((float) (j * xWidth + 60),
						(float) usedlist.get(j),
						(float) ((j + 1) * xWidth + 60),
						(float) usedlist.get(j + 1), mPaint);
//				myCanvas.drawLine((float) (j * xWidth + 60),
//						(float) usedlist.get(j),
//						(float) ((j + 1) * xWidth + 60),
//						(float) usedlist.get(j + 1), mPaint);
			}
		} else {
			for (int i = 0; i < values.length && usedlist.size() < listSize; i++) {
				int y = convertY2Pix(base, values[i]);
				usedlist.add(y);
			}
			for (int j = 0; j < usedlist.size() - 1; j++) {
				canvas.drawLine((float) (j * xWidth + 660),
						(float) usedlist.get(j),
						(float) ((j + 1) * xWidth + 660),
						(float) usedlist.get(j + 1), mPaint);
//				myCanvas.drawLine((float) (j * xWidth + 660),
//						(float) usedlist.get(j),
//						(float) ((j + 1) * xWidth + 660),
//						(float) usedlist.get(j + 1), mPaint);
			}
		}
		if (!isScreenshoted && usedlist.size() >= 359 && tag == 12) {
			// this.saveScreenshot();
			isScreenshoted = true;
		}
		// 当满屏后。清除缓存的数据。剩下只10个数据。
		if (usedlist.size() >= 359 && tag == 12) {
			int bytesSize = ecgBytesQueue.size();
			for (int i = 0; i < bytesSize - 10; i++) {
				ecgBytesQueue.poll();
			}
			int ecgSize = oneECGQueue.size();
			for (int i = 0; i < ecgSize - 10; i++) {
				oneECGQueue.poll();
			}
		}
	}

	private ArrayList<Integer> selectList(int tag) {
		switch (tag) {
		case 1:
			return usedlistI;
		case 2:
			return usedlistII;
		case 3:
			return usedlistIII;
		case 4:
			return usedlistaVR;
		case 5:
			return usedlistaVL;
		case 6:
			return usedlistaVF;
		case 7:
			return usedlistV1;
		case 8:
			return usedlistV2;
		case 9:
			return usedlistV3;
		case 10:
			return usedlistV4;
		case 11:
			return usedlistV5;
		case 12:
			return usedlistV6;
		default:
			return null;
		}
	}

	public double gain = 0.08;
	private ArrayList<int[]> contentEcgShow;
	private ReadQueueThread readQueueThread;

	private int convertY2Pix(int base, int y) {
		// 60/160=0.375
		double newValue = base + (-(y) * 0.667 * gain);
		int value = newValue >= 0 ? (int) (newValue + 0.5)
				: (int) (newValue - 0.5);
		// Log.i("value", "base =" + base + " " + "value =" + "" + value);
		return value;
	}

	/*
	 * private void alertDialog(AnalysisFrame frame) { ReadTxt2Map readTxt2Map =
	 * new ReadTxt2Map(context); readTxt2Map.readFromRaw(R.raw.map);
	 * StringBuilder builder = new StringBuilder(); HashMap<String, String> map
	 * = new HashMap<String, String>(); map.put("HR", frame.HR + "");
	 * map.put("PR", frame.PR + ""); map.put("PP", frame.PP + "");
	 * map.put("QRS", frame.QRS + ""); map.put("T", frame.T + ""); map.put("QT",
	 * frame.QT + ""); map.put("QTC", frame.QTC + ""); map.put("PAxial",
	 * frame.PAxial + ""); map.put("QRSAxial", frame.QRSAxial + "");
	 * map.put("TAxial", frame.TAxial + ""); map.put("SV1", frame.SV1 + "");
	 * map.put("PP", frame.PP + ""); int anaNum = frame.AnaNum; Log.i("anaNum",
	 * anaNum + ""); String anabuf = Arrays.toString(frame.AnaBuf); int[]
	 * resultArray = frame.AnaBuf; Log.i("frame.AnaBuf",
	 * Arrays.toString(frame.AnaBuf)); System.out.println(anabuf); //
	 * readTxt2Map.readFromRaw(R.raw.map); for (int i = 0; i < anaNum; i++) {
	 * builder.append(readTxt2Map.readFromRaw(R.raw.map, resultArray[i]));
	 * builder.append("\n"); } String result = builder.toString();
	 * map.put("anaResult", result); // handler.obtainMessage(DIALOG_FLAG,
	 * map).sendToTarget(); }
	 */

	public void setNowConfig() {
		this.xWidth = 0.8 / 2 / 3;
		this.listSize = 4200;
		this.gain = 0.2;
	}
}
